//
//  NMPOScanViewController.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOScanViewController: NMPOBaseViewController {

    init(nmpo_funcType: NMPOFuncType = .nmpo_speed) {
        super.init(nibName: nil, bundle: nil)
        self.nmpo_funcType = nmpo_funcType
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(nmpo_navView)
        view.addSubview(nmpo_loadingView)
        view.addSubview(nmpo_iconView)
        view.addSubview(nmpo_titleLabel)
        nmpo_loadingView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 270, height: 276))
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        nmpo_iconView.snp.makeConstraints { make in
            make.size.equalTo(146)
            make.centerX.centerY.equalTo(nmpo_loadingView)
        }
        nmpo_titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(nmpo_loadingView.snp.bottom).offset(52)
        }
        if nmpo_funcType == .nmpo_device {
            nmpo_startScanTimer()
            nmpo_titleLabel.isHidden = true
            nmpo_iconView.image = UIImage(named: "nmpo_scan_device")
        }else
        if nmpo_funcType == .nmpo_ip {
            nmpo_iconView.image = UIImage(named: "nmpo_scan_ip")
            NMPOManager.nmpo_manager.nmpo_requestIPInfoModel()
            nmpo_titleLabel.text = "Speeding up testing your IP.\nThis process takes a few seconds"
            NMPOManager.nmpo_manager.nmpo_delegate = self
        }else if nmpo_funcType == .nmpo_speed{
            NMPOManager.nmpo_manager.nmpo_delegate = self
            NMPOManager.nmpo_manager.nmpo_startSpeedScan()
            nmpo_speedValueModell = NMPOSpeedValueModel()
        }
        nmpo_navView.nmpo_titleLabel.text = nmpo_funcType.nmpo_title
    }
    // MARK: - nmpo lazy
    var nmpo_funcType: NMPOFuncType = .nmpo_speed
    override func nmpo_baseBgName() -> String? {
        return "nmpo_scan_bg"
    }
    lazy var nmpo_navView: NMPONavView = {
        let nmpo_navView = NMPONavView()
        nmpo_navView.nmpo_leftBlock = {[weak self] in
            NMPOManager.nmpo_manager.nmpo_delegate = nil
            NMPOManager.nmpo_manager.nmpo_stopSpeedScan()
            self?.nmpo_stopScanTimer()
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return nmpo_navView
    }()
    lazy var nmpo_loadingView: UIImageView = {
        let nmpo_loadingView = UIImageView(image: UIImage(named: "nmpo_scan_loading"))
        nmpo_loadingView.layer.add(nmpo_loadingKey, forKey: "nmpo_loadingKey")
        return nmpo_loadingView
    }()
    lazy var nmpo_loadingKey: CABasicAnimation = {
        let nmpo_loadingKey = CABasicAnimation.init(keyPath: "transform.rotation")
        nmpo_loadingKey.fromValue = NSNumber.init(value: 0)
        nmpo_loadingKey.toValue = NSNumber.init(value: Double.pi*2)
        nmpo_loadingKey.duration = 1
        nmpo_loadingKey.repeatCount = MAXFLOAT
        nmpo_loadingKey.isRemovedOnCompletion = false
        return nmpo_loadingKey
    }()
    lazy var nmpo_iconView: UIImageView = {
        let nmpo_iconView = UIImageView(image: UIImage(named: "nmpo_scan_speed"))
        return nmpo_iconView
    }()
    lazy var nmpo_titleLabel: UILabel = {
        let nmpo_titleLabel = UILabel()
        nmpo_titleLabel.font = UIFont.systemFont(ofSize: 16)
        nmpo_titleLabel.textColor = NMPOCommon.nmpo_getHXColorValue(rgb: 0xFFFFFF)
        nmpo_titleLabel.text = "Accelerating the testing of your network,\nthis process takes a few seconds."
        nmpo_titleLabel.numberOfLines = 0
        nmpo_titleLabel.textAlignment = .center
        return nmpo_titleLabel
    }()
    var nmpo_scanName: String = "nmpo_scanName"
    var nmpo_scanCount = 0
    var nmpo_speedValueModell: NMPOSpeedValueModel?
}
extension NMPOScanViewController {
    fileprivate func nmpo_startScanTimer() {
        nmpo_scanName = NMPOTimerVender.nmpo_timeTask(self, selector: #selector(nmpo_scanSum), start: 0, interval: 1, repeats: true, async: false, name: nmpo_scanName)
    }
    
    fileprivate func nmpo_stopScanTimer() {
        NMPOTimerVender.nmpo_cancelTask(nmpo_scanName)
        nmpo_scanName = "nmpo_scanName"
        nmpo_scanCount = 0
    }
    
    @objc fileprivate func nmpo_scanSum() {
        nmpo_scanCount = nmpo_scanCount + 1
        if nmpo_funcType == .nmpo_device && nmpo_scanCount > 2 {
            nmpo_stopScanTimer()
            var nmpo_resultModel = [NMPODeviceModel]()
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Device Name", nmpo_dec: UIDevice.current.name))
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Model", nmpo_dec: UIDevice.current.model))
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Version", nmpo_dec: UIDevice.current.systemVersion))
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Storage", nmpo_dec: NMPOCommon.nmpo_getSystemStorageSize()))
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Battery", nmpo_dec: "\(Int(UIDevice.current.batteryLevel * 100))%"))
            nmpo_resultModel.append(NMPODeviceModel(nmpo_title: "Model Number", nmpo_dec: UIDevice.current.identifierForVendor?.uuidString ?? ""))
            navigationController?.pushViewController(NMPODeviceResultViewController(nmpo_funcType: .nmpo_device, nmpo_resultModel:nmpo_resultModel), animated: true)
        }
        
    }
    
}

extension NMPOScanViewController: NMPOManagerDelegate {
    func nmpo_iPScanFinish(nmpo_model: NMPOIPScanModel?) {
        NMPOManager.nmpo_manager.nmpo_delegate = nil
        navigationController?.pushViewController(NMPOScanResultViewController(nmpo_funcType: .nmpo_ip,  nmpo_resultModel: nmpo_model), animated: true)
        NotificationCenter.default.removeObserver(self)
    }
    func nmpo_upLoadFinish(nmpo_value: String) {
        nmpo_speedValueModell?.nmpo_upload = nmpo_value
    }
    func nmpo_downloadFinish(nmpo_value: String) {
        nmpo_speedValueModell?.nmpo_download = nmpo_value
        
    }
    func nmpo_pingFinish(nmpo_ping: String, nmpo_jitter: String, nmpo_loss: String) {
        nmpo_speedValueModell?.nmpo_loss = nmpo_loss
        nmpo_speedValueModell?.nmpo_jitter = nmpo_jitter
        nmpo_speedValueModell?.nmpo_ping = nmpo_ping
        navigationController?.pushViewController(NMPOScanResultViewController(nmpo_funcType: .nmpo_speed,  nmpo_resultModel: nmpo_speedValueModell), animated: true)
        NotificationCenter.default.removeObserver(self)
    }
}
